package 寒假作业.LeetCode;

public class Num328 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode oddEvenList(ListNode head) {
        if (head == null) {
            return head;
        }
        //偶数节点头
        ListNode evenHead = head.next;
        //奇数节点
        ListNode odd = head;
        //偶数节点
        ListNode even = evenHead;
        while (even != null && even.next != null) {
            //迭代的时候，让奇数的next指向偶数的next，就可以保证奇数连接下一个奇数
            odd.next = even.next;
            //更新
            odd = odd.next;
            //同理
            even.next = odd.next;
            //更新
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }

}
